from bson import ObjectId
from pydantic import constr, Field, validator, field_validator
from typing_extensions import Optional

from common.pydantic_base_model import BaseModel


class UserSchema(BaseModel):
    username: str = Field(..., min_length=1, max_length=20)
    pwd: str = Field(..., min_length=4, max_length=10, strip_whitespace=True)
    role: str = Field('admin', description="角色:admin;user")
    desc: str = ''


class UserDeleteSchema(BaseModel):
    id: str

    @field_validator('id')
    def validate_id(cls, v):
        if isinstance(v, str):
            return ObjectId(v)
        return v


class UserLoginSchema(BaseModel):
    username: str = Field(..., min_length=1, max_length=20)
    pwd: str = Field(..., min_length=4, max_length=10, strip_whitespace=True)
